// 获取全局应用程序实例对象
const app = getApp()

// 创建页面实例对象
Page({
    /**
     * 页面的初始数据
     */
    data: {
        clubInfo: '',
        categorySport: [],
        categoryWeek: [],
        currentTag: 0,
        choicetype: "",
        clubId: '',
        lng: '',
        lat: '',
        isCollection: false
    },

    /**
     * 场馆照片预览
     */
    slideClub(options) {
        if (!options.target.dataset.number || !this.data.clubInfo.sysBusPictures[0].iconUrl) return;

        app.wechat.setStorage('slideClub', { name: this.data.clubInfo.clubName, list: this.data.clubInfo.sysBusPictures })
            .then(res => {
                wx.navigateTo({
                    url: '../slideClub/slideClub'
                })
            });
    },

    /**
     * 打开地图
     */

    openMap: function() {
        wx.openLocation({
                latitude: Number(this.data.lat), //res.latitude, // 纬度，范围为-90~90，负数表示南纬
                longitude: Number(this.data.lng), //res.longitude, // 经度，范围为-180~180，负数表示西经
                scale: 28, // 缩放比例          
            })
            // wx.getLocation({
            //     type: 'gcj02', // 默认为 wgs84 返回 gps 坐标，gcj02 返回可用于 wx.openLocation 的坐标
            //     success: function(res) {
            //         // success
            //         wx.openLocation({
            //             latitude: res.latitude, // 纬度，范围为-90~90，负数表示南纬
            //             longitude: res.longitude, // 经度，范围为-180~180，负数表示西经
            //             scale: 28, // 缩放比例          
            //         })
            //     }
            // })
    },

    /**
     * 选择运动类型
     */

    selectSport(option) {
        let currentTag = option.target.dataset.index;
        let choicetype = option.target.dataset.choicetype;
        this.setData({ currentTag: currentTag, choicetype: choicetype });
    },

    /**
     * 选择日期获取对应的票券
     */
    clickReservers(event) {
        let opts = {
            reserveItem: event.target.dataset.reserveItem,
            choicetype: this.data.choicetype
        }
        app.wechat.setStorage('orderReserve', opts)
            .then(res => {
                wx.navigateTo({
                    url: '../orderReserve/orderReserve'
                })
            });
    },

    /**
     * 拨打电话号码
     */
    call() {
        wx.makePhoneCall({
            phoneNumber: this.data.clubInfo.phone
        })
    },

    /**
     * 收藏or取消场馆
     */

    collectionClub() {

        if (this.data.isCollection) {
            app.yds.cancelCollection(this.data.clubId, app.globalData.sessionId)
                .then(res => {
                    if (res && res.code == 0) {
                        this.setData({ isCollection: false });
                        app.wechat.successToast(res.msg);
                    } else {
                        this.wetoast.toast({
                            duration: 2000,
                            title: res.msg,
                            img: '../../images/warn.png'
                        })
                    }
                });
        } else {
            app.yds.collection(this.data.clubId, app.globalData.sessionId)
                .then(res => {
                    if (res && res.code == 0) {
                        this.setData({ isCollection: true });
                        app.wechat.successToast(res.msg);
                    } else {
                        this.wetoast.toast({
                            duration: 2000,
                            title: res.msg,
                            img: '../../images/warn.png'
                        })
                    }
                });
        }
    },


    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        new app.WeToast()

        this.setData({ clubId: options.clubId, lng: options.lng, lat: options.lat });

        // app.wechat.loadingToast();
        let week = ['周天', '周一', '周二', '周三', '周四', '周五', '周六'];
        let daySecond = 60 * 60 * 24 * 1000;
        let tempArr = [];

        for (let i = 0; i < 7; i++) {
            let date = new Date(Date.parse(new Date()) + i * daySecond),
                month = app.util.helpNumber(date.getMonth() + 1),
                strDate = app.util.helpNumber(date.getDate()),
                weekIndex = date.getDay();
            let obj = {};
            obj.month = month;
            obj.strDate = strDate;
            obj.week = week[weekIndex];
            obj.clubId = this.data.clubId;
            tempArr.push(obj);
        }

        this.setData({ categoryWeek: tempArr });

        // let clubId = options.clubId;
        app.yds.getPbSportTypeByClubId(this.data.clubId)
            .then(res => {
                if (res && res.code == 0) {
                    this.setData({ categorySport: res.data });
                    this.setData({ choicetype: res.data[0] });
                    // wx.hideToast();
                } else {
                    this.wetoast.toast({
                        duration: 2000,
                        title: res.msg,
                        img: '../../images/warn.png'
                    })
                }
            });

        app.yds.getClubDetailInfo(this.data.clubId)
            .then(res => {
                if (res && res.code == 0) {
                    this.setData({ clubInfo: res.data });
                } else {
                    this.wetoast.toast({
                        duration: 2000,
                        title: res.msg,
                        img: '../../images/warn.png'
                    })
                }
            });

        app.yds.isCollection(this.data.clubId, app.globalData.sessionId)
            .then(res => {
                if (res && res.code == 0) {
                    if (res.data) {
                        this.setData({ isCollection: true });
                    } else {
                        this.setData({ isCollection: false });
                    }
                } else {
                    this.wetoast.toast({
                        duration: 2000,
                        title: res.msg,
                        img: '../../images/warn.png'
                    })
                }
            });

        // TODO: onLoad
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {
        // TODO: onReady
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        // TODO: onShow
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {
        // TODO: onHide
    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {
        // TODO: onUnload
    },
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {
        // TODO: onPullDownRefresh
    }
})
